import { Component, OnInit, AfterContentChecked } from '@angular/core';
import { NavParams, ModalController, LoadingController } from '@ionic/angular';
import { InstallService } from '@services/data.service/index';
import { MyNoteService } from '@services/myNote.service';
import { LoadingService } from '@services/loading.service';


@Component({
    selector: 'device-select',
    templateUrl: 'device-select.html',
    styleUrls: ['device-select.scss']
})
export class DeviceSelectModal implements OnInit, AfterContentChecked {
    deviceName;
    title;
    selected;
    constructor(
        public navParams: NavParams,
        public modalCtrl: ModalController,
        public installService: InstallService,
        public myNote: MyNoteService,
        public loadCtrl: LoadingController
    ) {
    }

    ngOnInit() {
        this.deviceName = this.navParams.data.device;
        this.title = this.navParams.data.title;
        this.selected = this.navParams.data.select;
        for (let i = 0; i < this.deviceName.length; i++) {
            if (this.deviceName[i].id) {
                if (this.deviceName[i].id === this.selected) {
                    this.deviceName[i].isSelected = true;
                } else {
                    this.deviceName[i].isSelected = false;
                }
            }
            if (this.deviceName[i].stackId) {
                if (this.deviceName[i].stackId === this.selected) {
                    this.deviceName[i].isSelected = true;
                } else {
                    this.deviceName[i].isSelected = false;
                }
            }
            if (!this.deviceName[i].mark) {
                this.deviceName[i].mark = true;
            }
            if (this.deviceName[i].mark && this.deviceName[i].mark === '未安装') {
                this.deviceName[i].mark = true;
            } else if (this.deviceName[i].mark && this.deviceName[i].mark === '已安装') {
                this.deviceName[i].mark = false;
            }
        }
    }

    ngAfterContentChecked() {

    }

    close() {
        this.modalCtrl.dismiss();
    }

    async selectDevice(item) {
        for (let i = 0; i < this.deviceName.length; i++) {
            this.deviceName[i].isSelected = false;
        }
        item.isSelected = true;
        console.log('关联数据栈', item)

        if (item.canCheck) {
            // 如果选择的数据栈存在并且已激活则向后台发起请求
            if (item.isActive) {
                const load = await this.loadCtrl.create({
                    message: '',
                    cssClass: 'custom-loading',
                    spinner: 'bubbles'
                });
                load.present();
                this.installService.getActivate(item.areaId, item.stackId, item.Code).subscribe(res => {
                    if (res.code === 200) {
                        load.dismiss();
                        this.modalCtrl.dismiss(item);
                    } else {
                        load.dismiss();
                        this.modalCtrl.dismiss(null);
                        setTimeout(() => {
                            this.myNote.MyNotice(res.message);
                        }, 1000);
                    }
                });
            } else {
                this.modalCtrl.dismiss(item);
            }
        } else {
            this.modalCtrl.dismiss(item);
        }
    }
}
